﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Newtonsoft.Json;

namespace GPS.Controllers.GPS_NghiepVu
{
    public class GPS_SoBaoDanhSatHachController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;

        #region Index


        public ActionResult Index(ViewModelHV_SBDSATHACH SearchModel, int? currentPage, FormCollection _form)
        {
            initialCategorySearchAction(SearchModel);

            var entities = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GETDS_HOCVIEN_SBD_SATHACH(SearchModel.pDVGTVT_MA
                                                                                               , SearchModel.pKH_MA
                                                                                               , SearchModel.pKH_LOP_MA
                                                                                               , SearchModel.pHV_MA
                                                                                               , SearchModel.pHV_HOTEN
                                                                                               , SearchModel.pHV_CMND
                                                                                               , SearchModel.pIS_SBD
                                                                                              );

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = entities.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            ViewBag.TotalRow = entities.Count();

            return View(SearchModel);
        }
        public void initialCategorySearchAction(ViewModelHV_SBDSATHACH viewModel)
        {

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");

         //   var _lst = _db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_LOAI == "DS" && o.DVGTVT_LOAI == "SH");
            ViewBag.DVSH_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && (o.DVGTVT_LOAI=="DS" || o.DVGTVT_LOAI=="SH")), "DVGTVT_MA", "DVGTVT_TEN");

            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;

            //KH_MAs
            var khoahocs = _db.Filter<DKH>(o => o.KH_DVGTVT_MA == viewModel.pDVGTVT_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");
            //KH_LOP_MAs
            var lophocs = _db.Filter<DKH_LOP>(o => o.KH_LOP_KH_MA == viewModel.pKH_MA);
            ViewBag.KH_LOP_MAs = new SelectList(lophocs, "KH_LOP_MA", "KH_LOP_MA");

        }
        #endregion

        #region"Approved"

        [HttpPost]
        public JsonResult Approved(JsonData1 a)
        {
            int _count = 0;
            if(a!=null)
            {
                if(a.Categories !=null && a.Categories.Length>0)
                {
                     int i = 0;
                    //lấy max sbd:

                     var _maxSBDs = _db.GetDbSet<DDTSH_TTSH>().Where(o => o.DTSH_TTSH_TTSH_MA == a.DTSH_TTSH_TTSH_MA).ToList();
                     if (_maxSBDs != null && _maxSBDs.Count>0)
                     {
                         var _maxSBD = _maxSBDs.Max(o => Convert.ToInt32((o.DTSH_TTSH_SBD)));
                         i = _maxSBD;
                     }
                    string[] Ids = JsonConvert.DeserializeObject<string[]>(a.Categories[0]);
                   
                    foreach (var _key in Ids)
                    {
                        i++;
                        var lstMA = _key.Split('#').ToList();
                        DDTSH_TTSH _itemInsert = new DDTSH_TTSH();
                        //var _DTSH_TTSH_MA = EFSequence.GetNextValue("SEQ_DDTSH_TTSH_MA");
                        _itemInsert.ID = EFSequence.GetNextValue("SEQ_DDTSH_TTSH_ID");
                        _itemInsert.DTSH_TTSH_MA = _itemInsert.ID.ToString();
                        _itemInsert.DTSH_TTSH_KH_LOP_MA = lstMA[0];
                        _itemInsert.DTSH_TTSH_DVGTVT_MA = lstMA[1];
                        _itemInsert.DTSH_TTSH_KH_MA = lstMA[2];
                        _itemInsert.DTSH_TTSH_HV_MA = lstMA[3];
                        _itemInsert.DTSH_TTSH_TTSH_MA = a.DTSH_TTSH_TTSH_MA;
                        _itemInsert.DTSH_TTSH_SOQDSH = a.DTSH_TTSH_SOQDSH;
                        _itemInsert.DTSH_TTSH_NGAYQDSH = a.DTSH_TTSH_NGAYQDSH;
                        _itemInsert.DTSH_TTSH_NGUOIQDSH = a.DTSH_TTSH_NGUOIQDSH;
                        _itemInsert.DTSH_TTSH_NGAYNHAP = DateTime.Now;
                        _itemInsert.DTSH_TTSH_NGUOINHAP = User.Identity.Name;
                        _itemInsert.DTSH_TTSH_SBD = (i < 10 ? "00" + i.ToString() : (i < 100 ? "0" + i.ToString() : i.ToString()));

                        //insert vao bảng DDTSH_TTSH
                        _db.Insert<DDTSH_TTSH>(_itemInsert);

                        _count++;
                    }
                }
            }
            

            TempData["ThongBao"] = string.Format("Đã phê duyệt [{0}] học viên thi sát hạch thành công!", _count);

            string url = Url.Action("Index", "GPS_HocVienSatHach");

            return Json(new { success = true, url = url });
            //   return Json("All the customers deleted successfully!");
        }

    
        public JsonResult UnApproved(string id1, string id2)
        {
            var DTSH_TTSH_MA = id1;
            var HV_MA = id2;
           // _db.Delete<DDTSH_TTSH>(o => o.DTSH_TTSH_MA == id1);
            Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_DELETE_SBD_SATHACH(id1);
            TempData["ThongBao"] = string.Format("Đã Hủy phê duyệt SBD thi sát hạch thành công  [{0}]  học viên!", HV_MA);
            return Json(true, JsonRequestBehavior.AllowGet);
        }


        public ActionResult AddStudentSBDSatHach(string id1, string id2, string id3)
        {
            try
            {
                string _KH_LOP_HV_KH_LOP_MA = id1;
                string _KH_LOP_HV_DVGTVT_MA = id2;
                string _KH_LOP_HV_KH_MA = id3;
                ViewBag.pDVGTVT_MA = id1;
                ViewBag.pKH_MA = id2;
                ViewBag.pKH_LOP_MA = id3;

                return PartialView("_AddStudentSatHach");
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }


        [HttpPost]
        public ActionResult AddStudentSBDSatHach(FormCollection _form)
        {

            int _count = 0;
            foreach (var _key in _form.AllKeys)
            {
                List<string> lstMA = null;
                if (_key.StartsWith("chk_"))
                {
                    lstMA = _form[_key].Split('#').ToList();
                    //   DKH_LOP_HV _itemInsert = new DKH_LOP_HV();


                    DDTSH_CSDT _itemInsert = new DDTSH_CSDT();
                  //  var _DDTSH_CSDT = EFSequence.GetNextValue("SEQ_DDTSH_CSDT_MA");
                    _itemInsert.ID = EFSequence.GetNextValue("SEQ_DDTSH_CSDT_ID");
                    _itemInsert.DTSH_CSDT_MA = _itemInsert.ID.ToString();
                    _itemInsert.DTSH_CSDT_KH_LOP_MA = lstMA[0];
                    _itemInsert.DTSH_CSDT_DVGTVT_MA = lstMA[1];
                    _itemInsert.DTSH_CSDT_KH_MA = lstMA[2];
                    _itemInsert.DTSH_CSDT_HV_MA = lstMA[3];

                    _itemInsert.DTSH_CSDT_NGAYNHAP = DateTime.Now;
                    _itemInsert.DTSH_CSDT_NGUOINHAP = User.Identity.Name;

                    //insert vao bảng DKH_LOP_HV
                    _db.Insert<DDTSH_CSDT>(_itemInsert);

                    _count++;
                }

            }

            TempData["ThongBao"] = string.Format("Đã thêm [{0}] học viên thi sát hạch thành công!", _count);

            string _pDVGTVT_MA = _form["pDVGTVT_MA"];
            string _pKH_MA = _form["pKH_MA"];
            string _pKH_LOP_MA = _form["pKH_LOP_MA"];

            string url = Url.Action("Index", "GPS_HocVienSatHach", new { pDVGTVT_MA = _pDVGTVT_MA, pKH_MA = _pKH_MA, pKH_LOP_MA = _pKH_LOP_MA });

            return Json(new { success = true, url = url });

        }

        #endregion

        #region"Ajax"

        [HttpPost]
        public ActionResult getHocVienKyTruoc(string keysearch)
        {
            var _keysearh = keysearch.ToUpper();
            var john = _db.Filter<DHV>(o => o.HV_SOCMT == _keysearh && (o.HV_KH_LOP_MA != "CXL" && o.HV_KH_LOP_MA != null)).Take(15).ToList();

            return Json(new { hocviens = john }, JsonRequestBehavior.AllowGet);

        }



        [HttpPost]
        public ActionResult getKH_MAs(string ma_DVGT)
        {
            var districts = _db.Filter<DKH>(d => d.KH_DVGTVT_MA == ma_DVGT).Select(a => "<option value='" + a.KH_MA + "'>" + a.KH_MA + "</option>");

            return Content(string.Join("", districts));
        }

        [HttpPost]
        public ActionResult getKH_LOP_MAs(string ma_KH)
        {
            var districts = _db.Filter<DKH_LOP>(d => d.KH_LOP_KH_MA == ma_KH).Select(a => "<option value='" + a.KH_LOP_MA + "'>" + a.KH_LOP_MA + "</option>");

            return Content(string.Join("", districts));
        }

        [HttpPost]
        public ActionResult getGV_MAs(string Ma_DVGT, string ma_LOP)
        {
            var districts = _db.Filter<DGV>(d => d.GV_DVGTVT_MA == Ma_DVGT).Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HODEM + " " + a.GV_TEN + "</option>");

            if (ma_LOP == null || string.IsNullOrEmpty(ma_LOP))
            {

                districts = _db.Filter<DGV>(d => d.GV_DVGTVT_MA == Ma_DVGT).Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HODEM + " " + a.GV_TEN + "</option>");

            }
            else
            {
                var MA_DGV = _db.GetOne<DKH_LOP>(o => o.KH_LOP_MA == ma_LOP).KH_LOP_GV_MA;
                districts = _db.Filter<DGV>(d => d.GV_DVGTVT_MA == Ma_DVGT && d.GV_MA == MA_DGV).Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HODEM + " " + a.GV_TEN + "</option>");

            }

            return Content(string.Join("", districts));
        }

        [HttpPost]
        public ActionResult getPT_MAs(string Ma_DVGT, string ma_HT, string ma_LOP)
        {
            var MA_DPT = _db.GetOne<DKH_LOP>(o => o.KH_LOP_MA == ma_LOP).KH_LOP_PT_MA;
            var districts = _db.Filter<DPT>(d => d.PT_DVGTVT_MA == Ma_DVGT && d.PT_BIENSO == MA_DPT).Select(a => "<option value='" + a.PT_BIENSO + "'>" + a.PT_BIENSO + "</option>");

            return Content(string.Join("", districts));
        }

        //  getHANGDTGP_MAs
        [HttpPost]
        public JsonResult getHANGDTGP_MAs(string ma_LOP)
        {

            var str1 = _db.GetOne<DKH_LOP>(o => o.KH_LOP_MA == ma_LOP);
            var str = str1.KH_LOP_HANGDT_MA + "," + str1.KH_LOP_HGPLX_MA;
            return Json(str);
        }


        //get Học viên
        [HttpPost]
        public ActionResult getHV_MAs(string Ma_DVGT, string ma_KH, string ma_LOP)
        {
            List<ModelDHV> _lst = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GetdataHocVienInLop(Ma_DVGT, ma_KH, ma_LOP);
            var districts = _lst.Select(a => "<option value='" + a.HV_MA + "'>" + a.HV_HODEM + " " + a.HV_TEN + "</option>"); ;

            return Content(string.Join("", districts));
        }

        #endregion
	}

    public class JsonData1
    {
        public string DTSH_TTSH_TTSH_MA { get; set; }
        public string DTSH_TTSH_SOQDSH { get; set; }
        public DateTime? DTSH_TTSH_NGAYQDSH { get; set; }
        public string DTSH_TTSH_NGUOIQDSH { get; set; }
         public string[] Categories { get; set; }
    }
    
}